import { markRaw } from 'vue';
import rxAjax from "@/assets/js/ajax.js";
import {BpmImageView} from '@lowcode/jpaas-share-component';
import {Util} from "@lowcode/jpaas-base-lib";
import DialogBox from "@/assets/js/dialogBox.js";


export default {
    methods:{
        /**
         * 打开表单组件。
         * @param conf
         *
         * this.open({
         *   component:'modules/form/customform/DemoOpen.vue',
         *   curVm:this.parentVm,
         *   max:true,
         *   widthHeight: ['800px', '600px'],
         *   title: '测试',
         *   data:{name:"ray",age:20}
         *   },function (action,data){
         *       console.info(data);
         *       debugger
         *   });
         *
         * @param destroy
         */
        open(conf,destroy){
            var data= conf.data;
            var component=conf.component;

            let dataObj={};

            for (let key in data) {
                let value=data[key];
                if(!value){
                    continue;
                }
                dataObj[key]=value;
            }
            if(component) {
                conf.component = this.getOpenDialogComponet(data);
                var obj=DialogBox.loadView(component);
                conf.data={currentView:obj,...dataObj};
                Util.open(conf,destroy);
            }else{
                alert(this.$ti18('fillInPath', '请填写相应组件路径！','formbaseApi'));
            }
        },
        getOpenDialogComponet(data){
            var template=`<div style="width:100%;height:100%;">
              <component v-if="!url" ref="current" :is="currentView" :layerid="layerid" :destroy="destroy" `;

            let ary=["currentView","url","layerid","destroy"];

            if(data) {

                for (var key in data) {
                    var value=data[key];
                    if(typeof value=='string' && value.constructor==String){
                        template += key + '="' + value + '" ';
                    }else{
                        template += ' :' + key + '="' + key + '" ';
                        ary.push( key );
                    }
                }
            }
            template +=`      ></component>
             <iframe v-else style="height:700px;width:100%;"
                      id="iframeId" :src="url" frameborder="0" scrolling="auto">
              </iframe>
           </div>`;

            let aryProp=[];
            for(let key of ary){
                aryProp.push('"' + key +'"');
            }
            let props=aryProp.join(",");


            var openDialogComponet = markRaw({
                template:template,
                ...eval('({\n' +
                    '        name: "DialogView",\n' +
                    '        props:['+props+']\n' +
                    '    })')
            })
            return openDialogComponet;
        },
        openUrl(conf,destroy){
            var url=conf.url;
            if(url) {
                conf.component = this.getOpenDialogComponet();
                conf.data={url:url};
                Util.open(conf,destroy);
            }else{
                alert(this.$ti18('fillInUrlPath', '请填写相应URL路径！','formbaseApi'));
            }
        },

        /**
         * 返回rxAjax
         * @returns {*}
         */
        getRxAjax(){
            return rxAjax;
        },
        /**
         * 打开流程图。
         */
        openFlowImg(){
            var defId=this.parentVm.localDefId;
            let obj = {
                widthHeight:['1024px','600px'],
            }
            if(this.isMobile){
                obj.max=true;
                delete obj.widthHeight
            }
            Util.open({
                component:BpmImageView,
                curVm:this.parentVm,
                ...obj,
                title:this.$ti18('bpmImage', '流程图','formBaseApi'),
                data:{
                    defId:defId,
                    formData:this.getFormData()
                }
            },function (action){
            });
        },
        //流程预演
        bpmPreview(){
            var defId=this.parentVm.localDefId;
            Util.open({
                component:BpmImageView,
                curVm:this.parentVm,
                title:this.$ti18('bpmPreview', '流程仿真','formBaseApi'),
                widthHeight:['1024px','600px'],
                data:{
                    defId:defId,
                    formData:this.getFormData(),
                    preview:true
                }
            },function (action){
            });
        }
    }
}
